package org.leaf.green.b2b

class PickplanController {

    def index = { redirect(action: "list", params: params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def pickplanService

    def list = {
        params.max = Math.min(params.max ? params.max.toInteger() : 100,  100)
        render view : '/b2b/purchase/pickplan/list', model : [pickplanInstanceList: pickplanService.list(params), pickplanInstanceTotal: pickplanService.count()]
    }

    def create = {
        def pickplanInstance = pickplanService.init(params)
        render view : '/b2b/purchase/pickplan/create', model : [pickplanInstance: pickplanInstance]
    }

    def save = {
        def pickplanInstance = new Purchase(params)
        if (!pickplanInstance.hasErrors() && pickplanInstance.save()) {
            flash.message = "pickplan.created"
            flash.args = [pickplanInstance.id]
            flash.defaultMessage = "Purchase ${pickplanInstance.id} created"
            redirect(action: "show", id: pickplanInstance.id)
        }
        else {
            render(view: "/b2b/purchase/pickplan/create", model: [pickplanInstance: pickplanInstance])
        }
    }

    def show = {
        def pickplanInstance = Purchase.get(params.id)
        if (!pickplanInstance) {
            flash.message = "pickplan.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Purchase not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            render view : '/b2b/purchase/pickplan/show', model : [pickplanInstance: pickplanInstance]
        }
    }

    def edit = {
        def pickplanInstance = Purchase.get(params.id)
        if (!pickplanInstance) {
            flash.message = "pickplan.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Purchase not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            render view : '/b2b/purchase/pickplan/edit', model : [pickplanInstance: pickplanInstance]
        }
    }

    def update = {
        def pickplanInstance = Purchase.get(params.id)
        if (pickplanInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (pickplanInstance.version > version) {
                    
                    pickplanInstance.errors.rejectValue("version", "pickplan.optimistic.locking.failure", "Another user has updated this Purchase while you were editing")
                    render(view: "/b2b/purchase/pickplan/edit", model: [pickplanInstance: pickplanInstance])
                    return
                }
            }
            pickplanInstance.properties = params
            if (!pickplanInstance.hasErrors() && pickplanInstance.save()) {
                flash.message = "pickplan.updated"
                flash.args = [params.id]
                flash.defaultMessage = "Purchase ${params.id} updated"
                redirect(action: "show", id: pickplanInstance.id)
            }
            else {
                render(view: "/b2b/purchase/pickplan/edit", model: [pickplanInstance: pickplanInstance])
            }
        }
        else {
            flash.message = "pickplan.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Purchase not found with id ${params.id}"
            redirect(action: "edit", id: params.id)
        }
    }

    def delete = {
        def pickplanInstance = Purchase.get(params.id)
        if (pickplanInstance) {
            try {
                pickplanInstance.delete()
                flash.message = "pickplan.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "Purchase ${params.id} deleted"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "pickplan.not.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "Purchase ${params.id} could not be deleted"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "pickplan.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Purchase not found with id ${params.id}"
            redirect(action: "list")
        }
    }
}
